package com.taobao.udp.udf;

import java.net.URI;
import java.util.Set;

import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.hooks.PreExecute;
import org.apache.hadoop.hive.ql.hooks.ReadEntity;
import org.apache.hadoop.hive.ql.hooks.WriteEntity;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.hadoop.hive.ql.session.SessionState.LogHelper;
import org.apache.hadoop.security.UserGroupInformation;

public class LoadJniResHook implements PreExecute { 

	@Override
	public void run(SessionState sess, Set<ReadEntity> inputs,
			Set<WriteEntity> outputs, UserGroupInformation ugi)
			throws Exception {
		// TODO Auto-generated method stub
		LogHelper cs = SessionState.getConsole();

		HiveConf conf = sess.getConf();
		boolean isRun = conf.getBoolean("hive.job.hooks.loadjni.enable", false);
		cs.printError("isRun is " + String.valueOf(isRun));
		if (isRun) {
			cs.printError("...............udp hooks start..............................");
			DistributedCache.createSymlink(conf);
			DistributedCache.addCacheArchive(new URI(
					"/group/tsc/commons/ws_1.3.0.9_3.4u7.jar#ws"), conf);
			conf.set("mapred.child.java.opts",
					"-Djava.library.path=./ws -Xmx2048m"); //设置内存上限
			cs.printError("...............udp hooks end...............................");
			cs.printError(conf.get("mapred.child.java.opts"));
		}
	}

}
